/*Copyright (C) 2013-2022 LZE*/
#include<bits/stdc++.h>
#define fo(x) freopen(#x".in", "r", stdin); freopen(#x".out", "w", stdout);
#define INF 0x7fffffff
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int N = 1000010;
const int M = 100010;
ll T, n, m;
struct TreeNode {
    ll val;
    ll lc, rc;
    ll size;
    TreeNode() {}
} bst[N];
ll cnt = 0, root;
void newNode(ll &now, ll val) {
    now = ++cnt;
    bst[now].val = val;
    bst[now].size = 1;
}
void insert(ll &now, ll val) {
    if(!now) {
        newNode(now, val);
        return;
    }
    bst[now].size++;
    if(val < bst[now].val) insert(bst[now].lc, val);
    else insert(bst[now].rc, val);
}
ll get_rank(ll &now, ll val) {
    if(!now) { return 0; }
    if(val == bst[now].val) return bst[bst[now].lc].size + 1;
    if(val < bst[now].val) return get_rank(bst[now].lc, val);
    else return get_rank(bst[now].rc, val) + bst[bst[now].lc].size + 1;
}
ll find_rank(ll &now, ll val) {
    if(!now) { return INF; }
    ll sz = bst[bst[now].lc].size;
    if(val == sz + 1) return bst[now].val;
    if(val <= sz) return find_rank(bst[now].lc, val);
    else return find_rank(bst[now].rc, val - sz - 1);
}
int main() {
    scanf("%lld", &n);
    ll op, x, t;
    for(ll i = 1; i <= n; i++) {
        scanf("%lld%lld", &op, &x);
        switch(op) {
            case 1 : { cout << get_rank(root, x - 1) + 1 << endl; break; }
            case 2 : { cout << find_rank(root, x) << endl; break; }
            case 3 : { t = get_rank(root, x - 1); cout << (t >= INF ? -INF : (find_rank(root, t) >= INF ? -INF : find_rank(root, t))) << endl; break; }
            case 4 : { t = get_rank(root, x) + 1; cout << (t >= INF ? INF : find_rank(root, t)) << endl; break; }
            case 5 : { insert(root, x); break; }
        }
    }
    return 0;
}
